/* js for search page */

$(document).ready(function() {
    var WAITING  = 2000;
    var keyword, main, sidebar, place;
    var resultMarkers = [];
    var map, bounds, selfMarker, options, center, lastZoomLevel;
    var homeLatitude, homeLongitude, homeLatLng;
    var lastLatitude, lastLongitude, lastLatLng;

    homeLatitude = utils.readCookie('homeLatitude');
    homeLongitude = utils.readCookie('homeLongitude');

    lastLatitude = utils.readCookie('lastLatitude') || homeLatitude;
    lastLongitude = utils.readCookie('lastLongitude') || homeLongitude;

    $('#button-search').click(function() {
        popup.create();

        main = $('#main');
        sidebar = $('#sidebar');
        keyword   = $('input#search').val();
        homeLatitude = utils.readCookie('homeLatitude');
        homeLongitude = utils.readCookie('homeLongitude');       

        if (this.timer) { clearTimeout(this.timer); }

        // Do ajax request
        this.timer = setTimeout(function() {
            $.ajax({
                url:'/search/place/' + keyword + '/' + 0,
                data: 'latitude='+homeLatitude+'&longitude='+homeLongitude,
                dataType: 'json',
                type: 'get',
                error:function() {
                  popup.remove('没有找到');
                },
                success:function (results) {
                  if (results.ok) {
                    popup.disable('Success');
                    main.html(utils.createResultsHTML(results));
                    sidebar.html(utils.createSideBarHTML(results));
                    //bounds = gmap.createBounds(results.places); 
                    center = new google.maps.LatLng(results.places[0].latitude, results.places[0].longitude);

                    options = {
                      center: center,
                      zoom: 10,
                      mapTypeId: google.maps.MapTypeId.ROADMAP,
                      disableDefaultUI: true,
                      navigationControl: true,
                      navigationControlOptions: {
                        position: google.maps.ControlPosition.RIGHT_BOTTOM,
                        style: google.maps.NavigationControlStyle.SMALL
                      }
                    };

                    map = new google.maps.Map(document.getElementById('map-search'), options);
                    if (results.places.length >= 2) {
                      bounds = gmap.createBounds(results.places); 
                      map.fitBounds(bounds);
                      map.setCenter(bounds.getCenter()); 
                    }

                    gmap.deleteOverlays(resultMarkers);
                    console.log('success');
                    console.log(utils.createResultsHTML(results));
                    for (var i = 0; i < results.places.length; i++) {
                      resultMarkers.push(gmap.createPlacesMarker(map, results.places[i], i));
                    }
                  } else {
                    popup.disable('Failed');
                  }
                }
            });                                                                                                        
          },
          WAITING);
      });

      $('button.search-index').live('click', function(event) {
          popup.create();

          main = $('#main');
          sidebar = $('#sidebar');
          url = $(this).attr('name');
          homeLatitude = utils.readCookie('homeLatitude');
          homeLongitude = utils.readCookie('homeLongitude');       

          console.log(url);
          if (this.timer) { clearTimeout(this.timer); }

          console.log('not');
          // Do ajax request
          this.timer = setTimeout(function() {
              $.ajax({
                  url: url,
                  data: 'latitude='+homeLatitude+'&longitude='+homeLongitude,
                  dataType: 'json',
                  type: 'get',
                  error:function() {
                    popup.remove('没有找到');
                  },
                  success:function (results) {
                    if (results.ok) {
                      popup.disable('Success');
                      main.html(utils.createResultsHTML(results));
                      sidebar.html(utils.createSideBarHTML(results));
                      //bounds = gmap.createBounds(results.places); 
                      center = new google.maps.LatLng(results.places[0].latitude, results.places[0].longitude);

                      options = {
                        center: center,
                        zoom: 10,
                        mapTypeId: google.maps.MapTypeId.ROADMAP,
                        disableDefaultUI: true,
                        navigationControl: true,
                        navigationControlOptions: {
                          position: google.maps.ControlPosition.RIGHT_BOTTOM,
                          style: google.maps.NavigationControlStyle.SMALL
                        }
                      };

                      map = new google.maps.Map(document.getElementById('map-search'), options);
                      if (results.places.length >= 2) {
                        bounds = gmap.createBounds(results.places); 
                        map.fitBounds(bounds);
                        map.setCenter(bounds.getCenter()); 
                      }                                           

                      gmap.deleteOverlays(resultMarkers);
                      console.log('success');
                      console.log(utils.createResultsHTML(results));
                      for (var i = 0; i < results.places.length; i++) {
                        resultMarkers.push(gmap.createPlacesMarker(map, results.places[i], i));
                      }
                    } else {
                      popup.disable('Failed');
                    }


                    // When hover on one result list, triggs a click event on correlation marker
                    $('#result0').mouseover( function() {
                        var marker = resultMarkers[0];
                        google.maps.event.trigger(marker, 'click'); 
                    }).mouseout(function() {
                        var marker = resultMarkers[0];
                        marker.infoBox.setMap(null);
                    });

                    $('#result1').mouseover( function() {
                        var marker = resultMarkers[1];
                        google.maps.event.trigger(marker, 'click'); 
                    }).mouseout(function() {
                        var marker = resultMarkers[1];
                        marker.infoBox.setMap(null);
                    });

                    $('#result2').mouseover( function() {
                        var marker = resultMarkers[2];
                        google.maps.event.trigger(marker, 'click'); 
                    }).mouseout(function() {
                        var marker = resultMarkers[2];
                        marker.infoBox.setMap(null);
                    });

                    $('#result3').mouseover( function() {
                        var marker = resultMarkers[3];
                        google.maps.event.trigger(marker, 'click'); 
                    }).mouseout(function() {
                        var marker = resultMarkers[3];
                        marker.infoBox.setMap(null);
                    });

                    $('#result4').mouseover( function() {
                        var marker = resultMarkers[4];
                        google.maps.event.trigger(marker, 'click'); 
                    }).mouseout(function() {
                        var marker = resultMarkers[4];
                        marker.infoBox.setMap(null);
                    });

                    $('#result5').mouseover( function() {
                        var marker = resultMarkers[5];
                        google.maps.event.trigger(marker, 'click'); 
                    }).mouseout(function() {
                        var marker = resultMarkers[5];
                        marker.infoBox.setMap(null);
                    });

                    $('#result6').mouseover( function() {
                        var marker = resultMarkers[6];
                        google.maps.event.trigger(marker, 'click'); 
                    }).mouseout(function() {
                        var marker = resultMarkers[6];
                        marker.infoBox.setMap(null);
                    });

                    $('#result7').mouseover( function() {
                        var marker = resultMarkers[7];
                        google.maps.event.trigger(marker, 'click'); 
                    }).mouseout(function() {
                        var marker = resultMarkers[7];
                        marker.infoBox.setMap(null);
                    });

                    $('#result8').mouseover( function() {
                        var marker = resultMarkers[8];
                        google.maps.event.trigger(marker, 'click'); 
                    }).mouseout(function() {
                        var marker = resultMarkers[8];
                        marker.infoBox.setMap(null);
                    });

                    $('#result9').mouseover( function() {
                        var marker = resultMarkers[9];
                        google.maps.event.trigger(marker, 'click'); 
                    }).mouseout(function() {
                        var marker = resultMarkers[9];
                        marker.infoBox.setMap(null);
                    });

                  }
              });                                                                                                        
            },
            WAITING);
        });

    });

    /*
    // When hover on one result list, triggs a click event on correlation marker
    $('#result0').live('mouseover', function() {
        console.log(WAITING);
        var marker = resultMarkers[0];
        console.log('infowindow');
        google.maps.event.trigger(marker, 'click'); 
    });*/
